home *** CD-ROM | disk | FTP | other *** search
/ Collection of Tools & Utilities / Collection of Tools and Utilities.iso / graphic / frasr182.zip / MPMATH_A.ASM < prev    next >
Assembly Source File  |  1992-12-19  |  17KB  |  1,060 lines

  1. TITLE mpmath_a.asm (C) 1989, Mark C. Peterson, CompuServe [70441,3353]
  2. SUBTTL All rights reserved.
  3. ;
  4. ;  Code may be used in any program provided the author is credited
  5. ;    either during program execution or in the documentation.  Source
  6. ;    code may be distributed only in combination with public domain or
  7. ;    shareware source code.  Source code may be modified provided the
  8. ;    copyright notice and this message is left unchanged and all
  9. ;    modifications are clearly documented.
  10. ;
  11. ;    I would appreciate a copy of any work which incorporates this code,
  12. ;    however this is optional.
  13. ;
  14. ;    Mark C. Peterson
  15. ;    405-C Queen St., Suite #181
  16. ;    Southington, CT 06489
  17. ;    (203) 276-9721
  18. ;
  19. ;  Note: Remark statements following floating point commands generally indicate
  20. ;     the FPU stack contents after the command is completed.
  21. ;
  22. ;  References:
  23. ;     80386/80286 Assembly Language Programming
  24. ;        by William H. Murray, III and Chris H. Pappas
  25. ;        Published by Osborne McGraw-Hill, 1986
  26. ;        
  27. ;
  28. ;
  29.  
  30.  
  31. IFDEF ??version
  32. MASM51
  33. QUIRKS
  34. ENDIF
  35.  
  36. .model medium, c
  37.  
  38.  
  39. .data
  40.  
  41. extrn cpu:WORD
  42.  
  43.  
  44. MP STRUC
  45.    Exp   DW    0
  46.    Mant  DD    0
  47. MP ENDS
  48.  
  49.  
  50. PUBLIC MPOverflow
  51. MPOverflow  DW        0
  52.  
  53. Ans         MP       <?>
  54. Double      DQ        ? 
  55.  
  56.  
  57. .code
  58.  
  59. .8086
  60.  
  61. fg2MP086    PROC     x:DWORD, fg:WORD
  62.    mov   ax, WORD PTR [x]
  63.    mov   dx, WORD PTR [x+2]
  64.    mov   cx, ax
  65.    or    cx, dx
  66.    jz    ExitFg2MP
  67.  
  68.    mov   cx, 1 SHL 14 + 30
  69.    sub   cx, fg
  70.  
  71.    or    dx, dx
  72.    jns   BitScanRight
  73.  
  74.    or    ch, 80h
  75.    not   ax
  76.    not   dx
  77.    add   ax, 1
  78.    adc   dx, 0
  79.  
  80. BitScanRight:
  81.    shl   ax, 1
  82.    rcl   dx, 1
  83.    dec   cx
  84.    or    dx, dx
  85.    jns   BitScanRight
  86.  
  87. ExitFg2MP:
  88.    mov   Ans.Exp, cx
  89.    mov   WORD PTR Ans.Mant+2, dx
  90.    mov   WORD PTR Ans.Mant, ax
  91.    lea   ax, Ans
  92.    mov   dx, ds
  93.    ret
  94. fg2MP086    ENDP
  95.  
  96.  
  97.  
  98. MPcmp086    PROC     uses si di, xExp:WORD, xMant:DWORD, yExp:WORD, yMant:DWORD
  99. LOCAL Rev:WORD, Flag:WORD
  100.    mov   Rev, 0
  101.    mov   Flag, 0
  102.    mov   ax, xExp
  103.    mov   dx, WORD PTR [xMant]
  104.    mov   si, WORD PTR [xMant+2]
  105.    mov   bx, yExp
  106.    mov   cx, WORD PTR [yMant]
  107.    mov   di, WORD PTR [yMant+2]
  108.    or    ax, ax
  109.    jns   AtLeastOnePos
  110.  
  111.    or    bx, bx
  112.    jns   AtLeastOnePos
  113.  
  114.    mov   Rev, 1
  115.    and   ah, 7fh
  116.    and   bh, 7fh
  117.  
  118. AtLeastOnePos:
  119.    cmp   ax, bx
  120.    jle   Cmp1
  121.  
  122.    mov   Flag, 1
  123.    jmp   ChkRev
  124.  
  125. Cmp1:
  126.    je    Cmp2
  127.  
  128.    mov   Flag, -1
  129.    jmp   ChkRev
  130.  
  131. Cmp2:
  132.    cmp   si, di
  133.    jbe   Cmp3
  134.  
  135.    mov   Flag, 1
  136.    jmp   ChkRev
  137.  
  138. Cmp3:
  139.    je    Cmp4
  140.  
  141.    mov   Flag, -1
  142.    jmp   ChkRev
  143.  
  144. Cmp4:
  145.    cmp   dx, cx
  146.    jbe   Cmp5
  147.  
  148.    mov   Flag, 1
  149.    jmp   ChkRev
  150.  
  151. Cmp5:
  152.    je    ChkRev
  153.  
  154.    mov   Flag, -1
  155.  
  156. ChkRev:
  157.     or    Rev, 0
  158.    jz    ExitCmp
  159.  
  160.    neg   Flag
  161.  
  162. ExitCmp:
  163.    mov   ax, Flag
  164.    ret
  165. MPcmp086    ENDP
  166.  
  167.  
  168.  
  169.  
  170.  
  171.  
  172. MPmul086    PROC     uses si di, xExp:WORD, xMant:DWORD, yExp:WORD, yMant:DWORD
  173.    mov   ax, xExp
  174.    mov   bx, yExp
  175.    xor   ch, ch
  176.    shl   bh, 1
  177.    rcr   ch, 1
  178.    shr   bh, 1
  179.    xor   ah, ch
  180.  
  181.    sub   bx, (1 SHL 14) - 2
  182.    add   ax, bx
  183.    jno   NoOverflow
  184.  
  185. Overflow:
  186.    or    WORD PTR [xMant+2], 0
  187.    jz    ZeroAns
  188.    or    WORD PTR [yMant+2], 0
  189.    jz    ZeroAns
  190.  
  191.    mov   MPOverflow, 1
  192.  
  193. ZeroAns:
  194.    xor   ax, ax
  195.    xor   dx, dx
  196.    mov   Ans.Exp, ax
  197.    jmp   StoreMant
  198.  
  199. NoOverflow:
  200.    mov   Ans.Exp, ax
  201.  
  202.    mov   si, WORD PTR [xMant+2]
  203.    mov   bx, WORD PTR [xMant]
  204.    mov   di, WORD PTR [yMant+2]
  205.    mov   cx, WORD PTR [yMant]
  206.  
  207.    mov   ax, si
  208.    or    ax, bx
  209.    jz    ZeroAns
  210.  
  211.    mov   ax, di
  212.    or    ax, cx
  213.    jz    ZeroAns
  214.  
  215.    mov   ax, cx
  216.    mul   bx
  217.    push  dx
  218.  
  219.    mov   ax, cx
  220.    mul   si
  221.    push  ax
  222.    push  dx
  223.  
  224.    mov   ax, bx
  225.    mul   di
  226.    push  ax
  227.    push  dx
  228.  
  229.    mov   ax, si
  230.    mul   di
  231.    pop   bx
  232.    pop   cx
  233.    pop   si
  234.    pop   di
  235.  
  236.    add   ax, bx
  237.    adc   dx, 0
  238.    pop   bx
  239.    add   di, bx
  240.    adc   ax, 0
  241.    adc   dx, 0
  242.    add   di, cx
  243.    adc   ax, si
  244.    adc   dx, 0
  245.  
  246.    or    dx, dx
  247.    js    StoreMant
  248.  
  249.    shl   di, 1
  250.    rcl   ax, 1
  251.    rcl   dx, 1
  252.    sub   Ans.Exp, 1
  253.    jo    Overflow
  254.  
  255. StoreMant:
  256.    mov   WORD PTR Ans.Mant+2, dx
  257.    mov   WORD PTR Ans.Mant, ax
  258.  
  259.    lea   ax, Ans
  260.    mov   dx, ds
  261.    ret
  262. MPmul086    ENDP
  263.  
  264.  
  265.  
  266. d2MP086     PROC     uses si di, x:QWORD
  267.    mov   dx, WORD PTR [x+6]
  268.    mov   ax, WORD PTR [x+4]
  269.    mov   bx, WORD PTR [x+2]
  270.    mov   cx, WORD PTR [x]
  271.    mov   si, dx
  272.    shl   si, 1
  273.    or    si, bx
  274.    or    si, ax
  275.    or    si, dx
  276.    or    si, cx
  277.    jnz   NonZero
  278.  
  279.    xor   ax, ax
  280.    xor   dx, dx
  281.    jmp   StoreAns
  282.  
  283. NonZero:
  284.    mov   si, dx
  285.    shl   si, 1
  286.    pushf
  287.    mov   cl, 4
  288.    shr   si, cl
  289.    popf
  290.    rcr   si, 1
  291.    add   si, (1 SHL 14) - (1 SHL 10)
  292.  
  293.    mov   di, ax                           ; shl dx:ax:bx 12 bits
  294.    mov   cl, 12
  295.    shl   dx, cl
  296.    shl   ax, cl
  297.    mov   cl, 4
  298.    shr   di, cl
  299.    shr   bx, cl
  300.    or    dx, di
  301.    or    ax, bx
  302.    stc
  303.    rcr   dx, 1
  304.    rcr   ax, 1
  305.  
  306. StoreAns:
  307.    mov   Ans.Exp, si
  308.    mov   WORD PTR Ans.Mant+2, dx
  309.    mov   WORD PTR Ans.Mant, ax
  310.  
  311.    lea   ax, Ans
  312.    mov   dx, ds
  313.    ret
  314. d2MP086     ENDP
  315.  
  316.  
  317.  
  318. MP2d086     PROC     uses si di, xExp:WORD, xMant:DWORD
  319.    sub   xExp, (1 SHL 14) - (1 SHL 10)
  320.    jo    Overflow
  321.  
  322.    mov   bx, xExp
  323.    and   bx, 0111100000000000b
  324.    jz    InRangeOfDouble
  325.  
  326. Overflow:
  327.    mov   MPOverflow, 1
  328.    xor   ax, ax
  329.    xor   dx, dx
  330.    xor   bx, bx
  331.    jmp   StoreAns
  332.  
  333. InRangeOfDouble:
  334.    mov   si, xExp
  335.    mov   ax, si
  336.    mov   cl, 5
  337.    shl   si, cl
  338.    shl   ax, 1
  339.    rcr   si, 1
  340.  
  341.    mov   dx, WORD PTR [xMant+2]
  342.    mov   ax, WORD PTR [xMant]
  343.    shl   ax, 1
  344.    rcl   dx, 1
  345.  
  346.    mov   bx, ax
  347.    mov   di, dx
  348.    mov   cl, 12
  349.    shr   dx, cl
  350.    shr   ax, cl
  351.    mov   cl, 4
  352.    shl   bx, cl
  353.    shl   di, cl
  354.    or    ax, di
  355.    or    dx, si
  356.  
  357. StoreAns:
  358.    mov   WORD PTR Double+6, dx
  359.    mov   WORD PTR Double+4, ax
  360.    mov   WORD PTR Double+2, bx
  361.    xor   bx, bx
  362.    mov   WORD PTR Double, bx
  363.  
  364.    lea   ax, Double
  365.    mov   dx, ds
  366.    ret
  367. MP2d086     ENDP
  368.  
  369.  
  370.  
  371.  
  372. MPadd086    PROC     uses si di, xExp:WORD, xMant:DWORD, yExp:WORD, yMant:DWORD
  373.    mov   si, xExp
  374.    mov   dx, WORD PTR [xMant+2]
  375.    mov   ax, WORD PTR [xMant]
  376.  
  377.    mov   di, yExp
  378.  
  379.    mov   cx, si
  380.    xor   cx, di
  381.    js    Subtract
  382.    jz    SameMag
  383.  
  384.    cmp   si, di
  385.    jg    XisGreater
  386.  
  387.    xchg  si, di
  388.    xchg  dx, WORD PTR [yMant+2]
  389.    xchg  ax, WORD PTR [yMant]
  390.  
  391. XisGreater:
  392.    mov   cx, si
  393.    sub   cx, di
  394.    cmp   cx, 32
  395.    jl    ChkSixteen
  396.    jmp   StoreAns
  397.  
  398. ChkSixteen:
  399.    cmp   cx, 16
  400.    jl    SixteenBitShift
  401.  
  402.    sub   cx, 16
  403.    mov   bx, WORD PTR [yMant+2]
  404.    shr   bx, cl
  405.    mov   WORD PTR [yMant], bx
  406.    mov   WORD PTR [yMant+2], 0
  407.    jmp   SameMag
  408.  
  409. SixteenBitShift:
  410.    mov   bx, WORD PTR [yMant+2]
  411.    shr   WORD PTR [yMant+2], cl
  412.    shr   WORD PTR [yMant], cl
  413.    neg   cl
  414.    add   cl, 16
  415.    shl   bx, cl
  416.    or    WORD PTR [yMant], bx
  417.  
  418. SameMag:
  419.    add   ax, WORD PTR [yMant]
  420.    adc   dx, WORD PTR [yMant+2]
  421.    jc    ShiftCarry
  422.    jmp   StoreAns
  423.  
  424. ShiftCarry:
  425.    rcr   dx, 1
  426.    rcr   ax, 1
  427.    add   si, 1
  428.    jo    Overflow
  429.    jmp   StoreAns
  430.  
  431. Overflow:
  432.    mov   MPOverflow, 1
  433.  
  434. ZeroAns:
  435.    xor   si, si
  436.    xor   ax, ax
  437.    xor   dx, dx
  438.    jmp   StoreAns
  439.  
  440. Subtract:
  441.    xor   di, 8000h
  442.    mov   cx, si
  443.    sub   cx, di
  444.    jnz   DifferentMag
  445.  
  446.    cmp   dx, WORD PTR [yMant+2]
  447.    jg    SubtractNumbers
  448.    jne   SwapNumbers
  449.  
  450.    cmp   ax, WORD PTR [yMant]
  451.    jg    SubtractNumbers
  452.    je    ZeroAns
  453.  
  454. SwapNumbers:   
  455.    xor   si, 8000h
  456.    xchg  ax, WORD PTR [yMant]
  457.    xchg  dx, WORD PTR [yMant+2]
  458.    jmp   SubtractNumbers
  459.  
  460. DifferentMag:
  461.    or    cx, cx
  462.    jns   NoSwap
  463.  
  464.    xchg  si, di
  465.    xchg  ax, WORD PTR [yMant]
  466.    xchg  dx, WORD PTR [yMant+2]
  467.    xor   si, 8000h
  468.    neg   cx
  469.  
  470. NoSwap:
  471.    cmp   cx, 32
  472.    jge   StoreAns
  473.  
  474.    cmp   cx, 16
  475.    jl    SixteenBitShift2
  476.  
  477.    sub   cx, 16
  478.    mov   bx, WORD PTR [yMant+2]
  479.    shr   bx, cl
  480.    mov   WORD PTR [yMant], bx
  481.    mov   WORD PTR [yMant+2], 0
  482.    jmp   SubtractNumbers
  483.  
  484. Sixt